﻿using System;
using System.Collections.Generic;
using System.Linq;
using Luna.Data.Configuration;
using Luna.Data.CRM;
using Luna.Model.CRM;
using Rhino.Mocks;

namespace Luna.Services.Test.Contacts
{
    public class CategoryTest : ReferenceEntityTest<IContactProvider, IEditableCategory, ICategoryData>
    {
        protected override Action<IContactProvider, IReferenceData> DataDelete
        {
            get { return (p, k) => p.CategoryDelete(k); }
        }

        protected override Func<IContactProvider, IReferenceData, ICategoryData> DataGet
        {
            get { return (p, k) => p.CategoryGet(k); }
        }

        protected override Func<IContactProvider, IEnumerable<ICategoryData>> DataGetAll
        {
            get { return p => p.CategoryGetAll(); }
        }

        protected override IContactProvider DataProvider
        {
            get { return DataContext.Contacts; }
        }

        protected override Action<IContactProvider, ICategoryData> DataSave
        {
            get { return (p, i) => p.CategorySave(i); }
        }

        protected override IEnumerable<IEditableCategory> EntitiesAll
        {
            get { return ApplicationModel.CRM.Categories.Select(x => x.EditableCopy); }
        }

        protected override Action<IEditableCategory> EntityDelete
        {
            get { return ApplicationModel.CRM.Delete; }
        }

        protected override Action<IEditableCategory> EntitySave
        {
            get { return ApplicationModel.CRM.Save; }
        }

        public override void SetUp()
        {
            base.SetUp();
            ApplicationModel.CRM.Reset();
            PropertyShifters.Add(new PropertyShifter<IEditableCategory, string>(x => x.Name, DataManager.GetString));
            PropertyShifters.Add(new PropertyShifter<IEditableCategory, string>(x => x.Description, DataManager.GetString));
        }

        protected override bool BuildMatch(IEditableCategory P_Entity, ICategoryData P_Data)
        {
            return base.BuildMatch(P_Entity, P_Data) && P_Entity.Name == P_Data.Name && P_Entity.Description == P_Data.Description;
        }

        protected override ICategoryData CreateMockData()
        {
            return ContactDataManager.CreateCategoryData();
        }

        protected override IEditableCategory EntityGet(Guid P_Key)
        {
            return ApplicationModel.CRM.Categories[P_Key].EditableCopy;
        }

        protected override ICategoryData SaveMatch(IEditableCategory P_Entity)
        {
            return Arg<ICategoryData>.Matches(
                 a => DataKeyMatch(a, P_Entity)
                     && a.Name == P_Entity.Name
                     && a.Description == P_Entity.Description);
        }
    }
}